package com.yy.controller.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.context.BaseContext;
import com.yy.controller.mapper.CategoryMapper;
import com.yy.controller.mapper.DishMapper;
import com.yy.controller.mapper.SetmealMapper;
import com.yy.controller.service.CategoryService;
import com.yy.dto.CategoryQueryDTO;
import com.yy.entity.Category;
import com.yy.entity.Dish;
import com.yy.entity.Setmeal;
import com.yy.exception.BaseException;
import com.yy.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类分页条件查询
     *
     * @param categoryQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(CategoryQueryDTO categoryQueryDTO) {
        //开始分页查询
        PageHelper.startPage(categoryQueryDTO.getPage(), categoryQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageList(categoryQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除分类
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        //查询菜品表看是否关联
        Integer dishNum = dishMapper.getByCategoryId(id);
        if (dishNum > 0) {
            throw new BaseException("该分类下有关联的菜品，无法删除");
        }
        //查询套餐表看是否关联
        Integer setmealNum = setmealMapper.getByCategoryId(id);
        if (setmealNum > 0) {
            throw new BaseException("该分类下有关联的套餐，无法删除");
        }
        //都不关联则删除
        categoryMapper.deleteById(id);
    }

    /**
     * 新增分类
     *
     * @param category
     */
    @Override
    public void add(Category category) {
        Long userId = BaseContext.getCurrentId();
        category.setCreateUser(userId);
        category.setUpdateUser(userId);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.add(category);
    }

    /**
     * 根据id修改分类
     *
     * @param category
     */
    @Override
    public void update(Category category) {
        Long userId = BaseContext.getCurrentId();
        category.setUpdateUser(userId);
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 根据id修改分类状态
     *
     * @param id
     * @param status
     */
    @Override
    public void editstatus(Long id, Integer status) {
        Category category = new Category();
        if (status == 1) {
            status = 0;
        } else if (status == 0) {
            status = 1;
        } else {
            throw new BaseException("该账号状态异常!");
        }
        category.setId(id);
        category.setStatus(status);
        //获取修改人 修改时间
        Long userId = BaseContext.getCurrentId();
        category.setUpdateUser(userId);
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.editeStatus(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> listByType(Integer type) {
        List<Category> categoryList = categoryMapper.listByType(type);
        return categoryList;
    }

    /**
     * 获取所有启用分类
     *
     * @return
     */
    @Override
    public List<Category> list() {
        //获取启用分类
        List<Category> categoryList = categoryMapper.listByStatus();
        //获取在售菜品列表和套餐列表
        for (Category category : categoryList) {
            if (category.getType() == 1) {
                List<Dish> dishList = dishMapper.dishListByCategoryIdandStatus(category.getId());
                category.setDishList(dishList);
            }
            if (category.getType() == 2) {
                List<Setmeal> setmealList = setmealMapper.setmealListByCategoryIdandStatus(category.getId());
                category.setSetmealList(setmealList);
            }
        }
        return categoryList;
    }
}
